﻿namespace Digitalmes.Domain.Mes;

/// <summary>
/// 设备警报记录
/// </summary>
[SugarTable("equip_alarm_record", "设备警报记录表")]
public class EquipAlarmRecord : EntityBaseId
{
    /// <summary>
    /// 产线编号
    /// </summary>
    [SugarColumn(ColumnDescription = "产线编号", Length = 32)]
    public string? LineCode { get; set; }

    /// <summary>
    /// 产线名称
    /// </summary>
    [SugarColumn(ColumnDescription = "产线名称", Length = 32)]
    public string? LineName { get; set; }

    /// <summary>
    /// 警报设编号，可为空
    /// </summary>
    [SugarColumn(ColumnDescription = "设备编号", Length = 64)]
    public string? EquipCode { get; init; }

    /// <summary>
    /// 警报设备名称，可为空
    /// </summary>
    [SugarColumn(ColumnDescription = "设备名称", Length = 64)]
    public string? EquipName { get; init; }

    /// <summary>
    /// 警报编号
    /// </summary>
    [SugarColumn(ColumnDescription = "警报编号")]
    public int Pos { get; init; }

    /// <summary>
    /// 警报内容
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "警报内容", Length = 64)]
    public string? Content { get; init; }

    /// <summary>
    /// 警报等级
    /// </summary>
    [SugarColumn(ColumnDescription = "警报等级")]
    public AlarmLevelEnum Level { get; init; }

    /// <summary>
    /// 警报分类
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "警报分类", Length = 32)]
    public string? Classify { get; init; }

    /// <summary>
    /// 警报开始时间
    /// </summary>
    [SugarColumn(ColumnDescription = "开始时间")]
    public DateTime StartTime { get; init; }

    /// <summary>
    /// 警报结束时间
    /// </summary>
    [SugarColumn(ColumnDescription = "结束时间")]
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 警报时长 (分钟)
    /// </summary>
    [SugarColumn(ColumnDescription = "警报时长", Length = 8, DecimalDigits = 1)]
    public double Duration { get; set; }

    /// <summary>
    /// 警报状态（1->警报中; 2->已关闭）
    /// </summary>
    [SugarColumn(ColumnDescription = "警报状态")]
    public int Status { get; set; }

    /// <summary>
    /// 警报关闭
    /// </summary>
    public void Close()
    {
        EndTime = DateTime.Now;
        Status = 2;
        Duration = Math.Round((EndTime! - StartTime).Value.TotalMinutes, 1);
    }
}
